package com.winai.launcher.base.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DBHelper extends SQLiteOpenHelper {

    //version number to upgrade database version
    //each time if you Add, Edit table, you need to change the
    //version number.
    //每次你对数据表进行编辑，添加时候，你都需要对数据库的版本进行提升
    //数据库版本public
    //数据库文件默认路径 data/data/<`package_name>/database/    ，如果要更改路径，需要先建立文件
    public static final int DATABASE_VERSION=2;
    //数据库名称
    public static final String DATABASE_NAME="tcm_runtime_record.db";

    private class ColumnAdd{
        ColumnAdd(String table,String column,String  type){
            strTable=table;strColumn=column;strType=type;
        }
        String strTable;
        String strColumn;
        String strType;
    }

    private String mCreateString;
    private String mTableName;
    private List<ColumnAdd> mListColumnAdd;


    public DBHelper(Context context,String strCreateString){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
        mCreateString=strCreateString;
    }

    //用这个初始化，则会根据版本删除表(一般开发期用)
    public DBHelper(Context context,String strCreateString,String strTableName){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
        mTableName=strTableName;
        mCreateString=strCreateString;
    }

    //不修改版本增加表(在创建后调用)
    public void addColumn(String strTableName,String strColumnName,String strColumnType){
        if(mListColumnAdd==null){
            mListColumnAdd=new ArrayList<>();
        }

        mListColumnAdd.add(new ColumnAdd(strTableName,strColumnName,strColumnType));
    }

    @Override
    public void onCreate(SQLiteDatabase db) {//数据库文件不存在时，创建数据库回调
        //创建数据表
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//当数据库版本变更的时候，会调用该函数
//        //如果旧表存在，删除，所以数据将会消失
//        db.execSQL("DROP TABLE IF EXISTS "+ Student.TABLE);
//
//        //再次创建表
//        onCreate(db);
        if(mTableName!=null && !mTableName.isEmpty()){
            db.execSQL("DROP TABLE IF EXISTS "+mTableName);
        }
    }

    @Override
    public void onOpen(SQLiteDatabase db){//多表结构，所以在这里创建
        super.onOpen(db);
        db.execSQL(mCreateString);

        if(mListColumnAdd!=null){
            for(ColumnAdd ca:mListColumnAdd){
                if(!isColumnExist(db,ca.strTable,ca.strColumn)){

                }
            }
        }
    }

    private boolean isColumnExist(SQLiteDatabase db,String strTable,String strColumnName){
        boolean result = false ;
        Cursor cursor = null ;
        try{
            cursor = db.rawQuery( "select * from sqlite_master where name = ? and sql like ?"
                    , new String[]{strTable , "%" + strColumnName + "%"} );
            result = null != cursor && cursor.moveToFirst() ;
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(null != cursor && !cursor.isClosed()){
                cursor.close();
            }
        }
        return result ;
    }

    private void addColumn(SQLiteDatabase db,String strTable,String strColumnName,String strColumnType){
        String strAddCo=String.format("ALTER  TABLE %s ADD COLUMN  %s %s",strTable,strColumnName,strColumnType );
        db.execSQL(strAddCo);
    }
}
